﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAOMall;

namespace DTOMall
{
    public class MoviesDTO : DBTool, ICommon
    {
        public bool insert(object obj)
        {
            if (obj == typeof(Movies))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Movies m = (Movies)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Insert_Movies";
                            cmd.Parameters.AddWithValue("@title", m.title);
                            cmd.Parameters.AddWithValue("@title_plain", m.title_plain);
                            cmd.Parameters.AddWithValue("@description", m.description);
                            cmd.Parameters.AddWithValue("@content", m.content);
                            cmd.Parameters.AddWithValue("@image", m.image);
                            cmd.Parameters.AddWithValue("@seats", m.seats);
                            cmd.Parameters.AddWithValue("@price", m.price);
                            cmd.Parameters.AddWithValue("@createdDate", m.created_date);
                            cmd.Parameters.AddWithValue("@status", m.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
            else
            {
                return false;
            }
        }

        public bool update(object obj)
        {
            if (obj == typeof(Products))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Movies m = (Movies)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Update_Movies";
                            cmd.Parameters.AddWithValue("@id", m.id);
                            cmd.Parameters.AddWithValue("@title", m.title);
                            cmd.Parameters.AddWithValue("@title_plain", m.title_plain);
                            cmd.Parameters.AddWithValue("@description", m.description);
                            cmd.Parameters.AddWithValue("@content", m.content);
                            cmd.Parameters.AddWithValue("@image", m.image);
                            cmd.Parameters.AddWithValue("@seats", m.seats);
                            cmd.Parameters.AddWithValue("@price", m.price);
                            cmd.Parameters.AddWithValue("@status", m.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                return false;
            }
        }

        public bool delete(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Delete_Movies";
                        cmd.Parameters.AddWithValue("@id", id);
                        object o = cmd.ExecuteScalar();
                        if (o == typeof(int))
                        {
                            return ((int)o > 0) ? true : false;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public System.Data.DataTable getAll()
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_All_Movies";
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public object getByID(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_Movies_By_ID";
                        cmd.Parameters.AddWithValue("@id", id);
                        DataTable dt = GetTable(cmd);
                        Movies m = new Movies();
                        m.id = id;
                        m.title = dt.Rows[0]["title"].ToString();
                        m.title_plain = dt.Rows[0]["title_plain"].ToString();
                        m.description = dt.Rows[0]["description"].ToString();
                        m.content = dt.Rows[0]["content"].ToString();
                        m.image = dt.Rows[0]["image"].ToString();
                        m.seats = Convert.ToInt32(dt.Rows[0]["seats"].ToString());
                        m.price = Convert.ToSingle(dt.Rows[0]["price"].ToString());
                        m.created_date = DateTimeLib.Convert2Date(dt.Rows[0]["created_date"].ToString());
                        m.status = Convert.ToInt32(dt.Rows[0]["status"].ToString());
                        return m;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #region SearchByName
        public DataTable Search(string title)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Search_Movie";
                        cmd.Parameters.AddWithValue("@title", title);
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
